package com.ltmall.api.manager.impl;

import com.ltmall.api.bean.ClientAccount;
import com.ltmall.api.bean.ClientInfo;
import com.ltmall.api.entity.info.MemberDeductibleInfo;
import com.ltmall.api.manager.AccountManager;
import com.ltmall.api.manager.MembershipManager;
import com.wuwangkeji.homeflavor.enums.MembershipEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MembershipManagerImpl implements MembershipManager {

    private AccountManager accountManager;

    @Override
    public MemberDeductibleInfo getDeductible(int userId) {

        //【1】判断用户是否为懂事特权用户
        boolean flag = true;
        ClientAccount clientAccount = accountManager.getAccount(null, userId);
        if (clientAccount.getPrivilegeExpiration() == null
            || clientAccount.getPrivilegeExpiration().before(new Date())) {
            flag = false;
        }
        
        if(clientAccount.getPrivilegeUsageCount()>0){
            flag = true; 
        }

        if (flag) {
            //优先使用董事特权抵扣
            return new MemberDeductibleInfo(MembershipEnum.PRIVILEGE_MEMBER, 0.9);
        }

        //【2】查询用户会员等级
        ClientInfo subUnderwriterInfo = accountManager.getInfo(userId);
        String level = subUnderwriterInfo.getLevelId() + "";
        if (MembershipEnum.PUBLIC_MEMBER.getCode().equals(level)) {
            //普通会员
            return new MemberDeductibleInfo(MembershipEnum.PUBLIC_MEMBER, 0.05);
        } else if (MembershipEnum.GOLD_MEMBER.getCode().equals(level)) {
            //黄金会员
            return new MemberDeductibleInfo(MembershipEnum.GOLD_MEMBER, 0.1);
        } else if (MembershipEnum.DIAMOND_MEMBER.getCode().equals(level)) {
            //钻石会员
            return new MemberDeductibleInfo(MembershipEnum.DIAMOND_MEMBER, 0.2);
        } else if (MembershipEnum.CROWN_MEMBER.getCode().equals(level)) {
            //皇冠会员
            return new MemberDeductibleInfo(MembershipEnum.CROWN_MEMBER, 0.4);
        } else {
            //默认
            return new MemberDeductibleInfo(MembershipEnum.PUBLIC_MEMBER, 0.05);
        }

    }

    /**
     * @param accountManager the accountManager to set
     */
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }
}
